<template>
  <view class="container">
    <!-- <view class="tab-item logout-btn" @click="logout">
        注销
      </view> -->
    <!-- 视图切换按钮区域 -->
    <view class="tab-bar">
      <view
        :class="['tab-item', currentView === 'classic' ? 'active' : '']"
        @click="switchView('classic')"
      >
        传统视图
      </view>
      <view
        :class="['tab-item', currentView === 'agent' ? 'active' : '']"
        @click="switchView('agent')"
      >
        AI视图
      </view>
      <!-- 新增注销按钮 -->
      
    </view>
    <view class="content">
      <classic-view v-if="currentView === 'classic'" :task-id="taskId" />
      <agent-view v-else />
    </view>
  </view>
</template>

<script>
import ClassicView from '@/components/classic.vue'
import AgentView from '@/components/agent.vue'

export default {
  components: {
    ClassicView,
    AgentView
  },
  data() {
    return {
      currentView: 'classic',
      taskId: null
    }
  },
  onLoad(options) {
    // 正确获取页面参数
    console.log("onLoad接收到的options:", options)
    if (options && options.task_id) {
      this.taskId = options.task_id
      console.log("从onLoad获取到task_id:", this.taskId)
    } else {
      console.log("未获取到task_id参数")
    }
  },
  watch: {
    taskId: {
      handler(newVal, oldVal) {
        console.log("taskId变化:", oldVal, "->", newVal)
      },
      immediate: true
    }
  },
  mounted() {
    console.log("mounted时的taskId:", this.taskId)
    const token = uni.getStorageSync("userInfo").token;
    if (!token) {
      uni.showToast({
        title: '请先登录',
        icon: 'none',
        duration: 2000
      })
      uni.redirectTo({
        url: "/pages/login",
      });
    } else {
      uni.request({
        url: './api/ct',
        method: 'GET',
        header: {
          'Authorization': `Bearer ${token}`
        },
        success: (res) => {
          if (!(res.statusCode === 200 && res.data.message === "合法")) {
            uni.showToast({
              title: '请先登录',
              icon: 'none',
              duration: 2000
            })
            uni.redirectTo({
              url: "/pages/login",
            });
          }
        }
      })
    }
  },
  methods: {
    switchView(view) {
      if (this.currentView === view) return
      this.currentView = view
    },
    logout() {
      // 清除本地存储的用户信息
      uni.removeStorageSync("userInfo");
      // 显示提示信息
      uni.showToast({
        title: '注销成功',
        icon: 'success',
        duration: 2000
      });
      // 跳转至登录页面
      uni.redirectTo({
        url: "/pages/login",
      });
    }
  }
}
</script>

<style scoped>
.container {
  display: flex;
  flex-direction: column;
  height: 100%;
  background-color: #f5f5f5;
}

/* 顶部切换栏 */
.tab-bar {
  display: flex;
  height: 50px;
  background-color: #ffffff;
  border-bottom: 1px solid #e0e0e0;
}
.tab-item {
  flex: 1;
  text-align: center;
  line-height: 50px;
  font-size: 16px;
  color: #666666;
}
.tab-item.active {
  color: #3498ff;
  border-bottom: 2px solid #3498ff;
}

/* 注销按钮靠右 */
.logout-btn {
  position: absolute;
  right: 10px;
  top: 10px;
  font-size: 16px;
  color: #ff4444;
}

/* 内容区占满剩余空间 */
.content {
  flex: 1;
  position: relative;
}
</style>
